package shiro.com.controller.file;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.http.HttpResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import shiro.com.basie.domain.UpdateFile;
import shiro.com.domain.sys.SysUsers;
import shiro.com.service.sys.ISysUsersService;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


@RestController
@RequestMapping("/updown")
public class OssFileController {

    @Autowired
    private ISysUsersService sysUsersService;

         @PostMapping(value = "/updateImgFile")
        public String updateImgFile(@RequestParam(value = "userId") String userId ,@RequestPart MultipartFile file) throws IOException {
             String endpoint = "oss-cn-guangzhou.aliyuncs.com";
             // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
             String accessKeyId = "LTAI5tQYeeASCg5RDfwDg8HK";
             String accessKeySecret = "zYriighPTjBBBBsSfh2HztBWgaF7TH";

             // 创建OSSClient实例。
             OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

             // 上传文件流。
             InputStream inputStream =file.getInputStream();
             Date date = new Date();
             String strDateFormat = "yyyy-MM-dd";
             SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
             String str = sdf.format(date);


             //文件名称
             String preUuid = UUID.randomUUID().toString();
             System.out.println(preUuid);

             //第一种方法生成UUID，去掉“-”符号
             String uuid = UUID.randomUUID().toString().replace("-", "");

             SysUsers sysUsers = sysUsersService.selectSysUsersBySysUserId(Long.valueOf(userId));
             Long sysUserPhone = sysUsers.getSysUserPhone();
             String fileUrl = str + "/clock/" + sysUserPhone + "/" + uuid + "-" + file.getOriginalFilename();
             PutObjectResult putObjectResult = ossClient.putObject("gaolenlin-gulimall", fileUrl, inputStream);

             // 关闭OSSClient。
             ossClient.shutdown();
             //返回路径
             String filePath="https://gaolenlin-gulimall"+"."+endpoint+"/"+fileUrl;

             System.out.println("上传成功...");

             return filePath;

         }

       @PostMapping(value = "/teacher/updateFile")
         public String updateImgFile2(@RequestParam(value = "userId") String userId,@RequestPart MultipartFile file) throws IOException {
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI5tQYeeASCg5RDfwDg8HK";
        String accessKeySecret = "zYriighPTjBBBBsSfh2HztBWgaF7TH";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件流。
        InputStream inputStream =file.getInputStream();
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String str = sdf.format(date);


        //文件名称
        String preUuid = UUID.randomUUID().toString();
        System.out.println(preUuid);

        //第一种方法生成UUID，去掉“-”符号
        String uuid = UUID.randomUUID().toString().replace("-", "");

        SysUsers sysUsers = sysUsersService.selectSysUsersBySysUserId(Long.valueOf(userId));
        Long sysUserPhone = sysUsers.getSysUserPhone();
        String fileUrl = str + "/clock/" + sysUserPhone + "/"+file.getOriginalFilename().split("\\.")[0] +"/" + file.getOriginalFilename().split("\\.")[0] + "-" + uuid+"."+ file.getOriginalFilename().split("\\.")[1];
        PutObjectResult putObjectResult = ossClient.putObject("gaoolenlin-bishe-file", fileUrl, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
        //返回路径
        String filePath="https://gaoolenlin-bishe-file"+"."+endpoint+"/"+fileUrl;

        System.out.println("上传成功...");

        return filePath;}


    @PostMapping(value = "/student/updateFile")
    public String updateImgFile3(@RequestParam(value = "userId") String userId,@RequestParam(value = "dateTime") String dateTime,  @RequestPart MultipartFile file) throws IOException {
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI5tQYeeASCg5RDfwDg8HK";
        String accessKeySecret = "zYriighPTjBBBBsSfh2HztBWgaF7TH";


        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件流。
        InputStream inputStream =file.getInputStream();



        //文件名称
        String preUuid = UUID.randomUUID().toString();
        System.out.println(preUuid);

        //第一种方法生成UUID，去掉“-”符号
        String uuid = UUID.randomUUID().toString().replace("-", "");

        SysUsers sysUsers = sysUsersService.selectSysUsersBySysUserId(Long.valueOf(userId));
        Long sysUserPhone = sysUsers.getSysUserPhone();

        String fileUrl = dateTime + "/clock/" + sysUserPhone + "/"+file.getOriginalFilename().split("-")[1].split("\\.")[0] +"/"+ file.getOriginalFilename().split("\\.")[0] + "-" + uuid+"."+ file.getOriginalFilename().split("\\.")[1];
        PutObjectResult putObjectResult = ossClient.putObject("gaoolenlin-bishe-file", fileUrl, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
        //返回路径
        String filePath="https://gaoolenlin-bishe-file"+"."+endpoint+"/"+fileUrl;

        System.out.println("上传成功...");

        return filePath;}


}

